package com.ant.business.ocr.controller;

import com.ant.common.annotation.AuthIgnore;
import com.github.pagehelper.PageInfo;
import com.ant.common.utils.FastMap;
import com.ant.common.result.ResponseResult;
import com.ant.common.exceptions.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.ant.business.ocr.entity.Medicine;
import com.ant.business.ocr.service.MedicineService;

import java.util.List;
import java.util.Objects;

/**
 * 药品表控制层
 *
 * @description
 * @create 2023-08-17 16:25:53
 */
@RestController
@RequestMapping(value = "/medicine")
@Slf4j
@Api(tags = "药品表")
public class MedicineController {

    private final MedicineService medicineService;

    public MedicineController(MedicineService medicineService) {
        this.medicineService = medicineService;
    }

    /**
     * 药品表分页查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/medicinePage", name = "药品表分页查询列表")
    @ApiOperation("药品表分页查询列表")
    @AuthIgnore
    public ResponseResult medicinePage(@RequestBody FastMap param){
        try {
            PageInfo<Medicine> pages = medicineService.getList(param.getInt("page"), param.getInt("limit"), param);
            log.info("药品表分页查询列表成功");
            return ResponseResult.success("药品表分页查询列表成功", pages.getList(), pages.getTotal());
        } catch (Exception e) {
            log.info("药品表分页查询列表失败：", e);
            return ResponseResult.fail("药品表分页查询列表失败：" + e.getMessage());
        }
    }

    /**
     * 药品表查询列表
     *
     * @param param 条件参数
     * @return ResponseResult
     */
    @PostMapping(value = "/allList", name = "药品表查询列表")
    @ApiOperation("药品表查询列表")
    @AuthIgnore
    public ResponseResult allList(@RequestBody FastMap param){
        try {
            List<Medicine> list = medicineService.getAllList(param);
            log.info("药品表查询列表成功");
            return ResponseResult.success("药品表查询列表成功", list);
        } catch (Exception e) {
            log.info("药品表查询列表失败：", e);
            return ResponseResult.fail("药品表查询列表失败：" + e.getMessage());
        }
    }

    /**
     * 药品表通过编号查询
     *
     * @param primaryKey
     * @return ResponseResult
     */
    @PostMapping(value = "/getById", name = "药品表通过编号查询")
    @ApiOperation("药品表通过编号查询")
    @AuthIgnore
    public ResponseResult getById(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到药品表编号");
            }

            Medicine medicine = medicineService.getById(primaryKey);
            log.info("药品表通过编号查询成功");
            return ResponseResult.success("药品表通过编号查询成功", medicine);
        } catch (Exception e) {
            log.info("药品表通过编号查询失败：", e);
            return ResponseResult.fail("药品表通过编号查询失败：" + e.getMessage());
        }
    }

    /**
     * 修改药品表（新增、修改）
     *
     * @param medicine medicine
     * @return ResponseResult
     */
    @PostMapping(value = "/edit", name = "修改药品表")
    @ApiOperation("修改药品表（新增、修改）")
    @AuthIgnore
    public ResponseResult edit(@RequestBody Medicine medicine){
        try {
            if (Objects.isNull(medicine.getId()) || medicine.getId()<=0){
                //新增
                medicineService.saveOne(medicine);
                log.info("药品表新增成功");
            }else {
                //编辑
                medicineService.update(medicine);
                log.info("药品表修改成功");
            }
            return ResponseResult.success("药品表编辑成功");
        } catch (Exception e) {
            log.info("药品表编辑失败：", e);
            return ResponseResult.fail("药品表编辑失败：" + e.getMessage());
        }
    }

    @PostMapping(value = "/batchEdit", name = "批量修改或新增药品表")
    @ApiOperation("批量修改药品表（新增、修改）")
    @AuthIgnore
    public ResponseResult batchEdit(@RequestBody List<Medicine> medicines){
        for(Medicine medicine : medicines){
            try {
                if (Objects.isNull(medicine.getId()) || medicine.getId()<=0){
                    //新增
                    medicineService.saveOne(medicine);
                    log.info("药品表新增成功");
                }else {
                    //编辑
                    medicineService.update(medicine);
                    log.info("药品表修改成功");
                }
            } catch (Exception e) {
                log.info("药品表编辑失败：", e);
            }

        }
         return ResponseResult.success("操作成功");
    }

    /**
     * 根据id删除药品表
     *
     * @param primaryKey id
     * @return ResponseResult
     */
    @PostMapping(value = "/delete", name = "删除药品表")
    @ApiOperation("删除药品表")
    @AuthIgnore
    public ResponseResult delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到药品表编号");
            }
            medicineService.delete(primaryKey);
            log.info("药品表删除成功");
            return ResponseResult.success("药品表删除成功");
        } catch (Exception e) {
            log.info("药品表删除失败：", e);
            return ResponseResult.fail("药品表删除失败：" + e.getMessage());
        }
    }
}
